using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Decav.Gatsb;

/// <summary>
/// Changes privacy settings using a slider control.
/// </summary>
public partial class PrivacySliderControl : System.Web.UI.UserControl
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        sliderValue.TextChanged += new EventHandler(sliderValue_TextChanged);
    }

    void sliderValue_TextChanged(object sender, EventArgs e)
    {
        if (ValueChanged != null)
            ValueChanged(this, e);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        sliderValue.Attributes.Add("onchange", SliderUpdateEventJavascript);
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UpdateJs", UpdateJavascript, true);
    }

    /// <summary>
    /// Raised when the value of the control is changed.
    /// </summary>
    public event EventHandler ValueChanged;

    /// <summary>
    /// Gets or sets the selected permission level.
    /// </summary>
    public PrivacyPermissionLevel SelectedValue
    {
        get
        {
            if (sliderValue.Text == string.Empty)
                return PrivacyPermissionLevel.Friends;

            int i = 0;
            if (Int32.TryParse(sliderValue.Text, out i))
            {
                switch (i)
                {
                    case 0:
                        return PrivacyPermissionLevel.Private;

                    case 1:
                        return PrivacyPermissionLevel.Friends;

                    case 2:
                        return PrivacyPermissionLevel.Public;
                }
            }

            throw new InvalidOperationException("The sliderValue text box has a non-integer value.");
        }
        set
        {
            switch (value)
            {
                case PrivacyPermissionLevel.Private:
                    sliderValue.Text = "0";
                    break;

                case PrivacyPermissionLevel.Friends:
                    sliderValue.Text = "1";
                    break;

                case PrivacyPermissionLevel.Public:
                    sliderValue.Text = "2";
                    break;
                     
                default:
                    throw new ArgumentException("The specified PrivacyPermissionLevel is not valid for the PrivacySliderControl.");
            }
        }
    }

    protected string LabelValue
    {
        get
        {
            switch (SelectedValue)
            {
                case PrivacyPermissionLevel.Friends:
                    return "Friends (my friends can see this)";
                case PrivacyPermissionLevel.Private:
                    return "Private (only I can see this)";
                case PrivacyPermissionLevel.Public:
                    return "Public (everyone can see this)";
                default:
                    throw new NotImplementedException();
            }
        }
    }

    private const string SliderUpdateEventJavascript = "UpdateSliderLabel(this.id, this.value)";
    private const string UpdateJavascript = @"function UpdateSliderLabel(tbid, tbvalue)
        {
            var lbl = document.getElementById(tbid + ""_Label""); 
         
            if (tbvalue == 0)
                lbl.innerText = ""Private (only I can see this)"";
            else if (tbvalue == 1)
                lbl.innerText = ""Friends (my friends can see this)"";
            else
                lbl.innerText = ""Public (everyone can see this)"";
        }";
}
